﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace Weka
{
    internal class ObjectSerialiser { }


    internal class ObjectSerialiser<T> : ObjectSerialiser
    {
        public MessageIdAttribute MessageId { get; internal set; }
        internal FieldSerialiser<T>[] _fieldSerialisers;


        internal void ToData(T value, StreamWrita streamWriter)
        {
            WriteMessageId(streamWriter);
            WriteFieldMetaData(streamWriter);
            WriteFieldData(value, streamWriter);
        }

        public void WriteFieldData(T value, StreamWrita streamWriter)
        {
            WriteFieldHeaders(value, streamWriter);
            WriteFieldBody(streamWriter);
        }

        public void WriteMessageId(StreamWrita streamWriter)
        {
            streamWriter.Write(MessageId);
        }

        private void WriteFieldBody(StreamWrita streamWriter)
        {
            for (int i = 0; i < _fieldSerialisers.Length; i++)
                _fieldSerialisers[i].WriteValue(streamWriter);
        }

        private void WriteFieldHeaders(T value, StreamWrita streamWriter)
        {
            for (int i = 0; i < _fieldSerialisers.Length; i++)
                _fieldSerialisers[i].ReadValueFromTAndWriteHeader(value, streamWriter);
            streamWriter.HeaderCommit();
        }

        public void WriteFieldMetaData(StreamWrita streamWriter)
        {
            streamWriter.Write((byte)_fieldSerialisers.Length);
            for (int i = 0; i < _fieldSerialisers.Length; i++)
                _fieldSerialisers[i].WriteMetadata(streamWriter);
        }
    }




}
